<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'threadlistener.jsp' starting page</title>

 <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
      <link rel="stylesheet" type="text/css" href="style/other/easyui.css">
    <link rel="stylesheet" type="text/css" href="style/other/icon.css">
    <link rel="stylesheet" type="text/css" href="style/other/demo.css">
    <script type="text/javascript" src="style/js/jquery-1.8.1.min.js"></script>
    <script type="text/javascript" src="style/js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="style/js/my.js"></script>
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>

<body>
	<script type="text/javascript">
	
	
	function closeBeatThread(name){
		console.log("closeBeatThread ======= "+name);
		$.ajax({
				url:"<%=basePath%>admin/user_closeBeatThread.action",
				datatype:"json",
				data:{threadName:name},
				type:"post",
				async:"true",
			}).success(function(data){
				getThread();
			}).error();
	}
	
	function closeThread(name){
		console.log("close ======= "+name);
		$.ajax({
				url:"<%=basePath%>admin/user_closeThread.action",
				datatype:"json",
				data:{threadName:name},
				type:"post",
				async:"true",
			}).success(function(data){
				getThread();
			}).error();
	}
	
	function reopen(name){
		console.log("reopen ======= "+name);
		$.ajax({
				url:"<%=basePath%>admin/user_reopen.action",
				datatype:"json",
				data:{threadName:name},
				type:"post",
				async:"true",
			}).success(function(data){
				getThread();
			}).error();
	}
	
	function reBeat(name){
		console.log("reopen ======= "+name);
		$.ajax({
				url:"<%=basePath%>admin/user_reBeat.action",
				datatype:"json",
				data:{threadName:name},
				type:"post",
				async:"true",
			}).success(function(data){
				getThread();
			}).error();
	}
	
 	function getThread(){
			$.ajax({
				url:"<%=basePath%>admin/user_getThread.action",
				datatype : "json",
				type : "post",
				async : "true",
			}).success(
					function(data) {
						 console.log(data);
						 var thradNameList = eval(data);
						 $("#thradlist").html("");
						 $("#beatthradlist").html("");
						 
						 $.each(thradNameList[0],function(index,value){
							 
					            $("#thradlist").append(
					            	"<tr>"+
					    				"<td>"+value.type+":"+value.uin+"</td>"+
					    				"<td>"+value.beat+"</td>"+
					    				"<td>"+
					    				"<button onclick=\"reopen('"+value.threadName+"')\">重启</button>"+
					    				"<button onclick=\"closeThread('"+value.threadName+"')\">关闭</button>"+
					    				"</td>"+
					    			"</tr>");
					        });
						 
						 $.each(thradNameList[1],function(index,value){
					            $("#beatthradlist").append(
					            	"<tr>"+
					    				"<td>"+value.type+":"+value.threadName+"</td>"+
					    				"<td>"+value.beat+"</td>"+
						    				"<td>"+
					    				"<button onclick=\"reBeat('"+value.threadName+"')\">重启</button>"+
					    				"<button onclick=\"closeBeatThread('"+value.threadName+"')\">关闭</button>"+
					    				"</td>"+
					    			"</tr>");
					        });
					}).error(function(data) {
			});
		}
 		getThread();
		setInterval(getThread, 10000);
		
	</script>
	
	
	<div class="easyui-layout" fit="true" style="height:200px;">
    
		
		<div id="content" region="center" title="发送信息" style="padding:20px;">
		
		
    	<table id="dg" title="微信线程列表" style="width:auto;height:520px" data-options="
    				rownumbers:true,
    				singleSelect:true,
    				autoRowHeight:false,
    				pagination:true,
    				pageSize:20">
    		<thead>
    			<tr style="width:100%">
    				<th  field="Uin" width="150" align="center">微信号</th>
    				<th  field="nickname" width="150" align="center">微信别名</th>
    				<th  field="threadName" width="150" align="center">线程名</th>
    				<th  field="time" width="200" align="center">启动时间</th>
    				<th  field="beat" width="200" align="center">状态</th>
    				<th  field="stats" width="110" align="center">操作</th>
    			</tr>
    		</thead>
    	</table>
    	</div>
    	</div>
    	<script>
            function getData(json){
    
            var rows = [];
            
            $.each(json[0],function(index,value){

                rows.push({
                	Uin :value.uin,
                	nickname: value.nickname,
                    threadName: value.type,
                    beat:value.beat,
                    time:value.time,
                    stats: "<button onclick=\"reopen('"+value.threadName+"')\">重启</button>   <button onclick=\"closeThread('"+value.threadName+"')\">关闭</button>",
                    note:0
                });
	           
            });
            
            
            $.each(json[1],function(index,value){
            	 rows.push({
                 	Uin :value.uin,
                 	 nickname: value.nickname,
                     threadName: value.type,
                     beat:value.beat,
                     time:value.time,
                     stats: "<button onclick=\"reopen('"+value.threadName+"')\">重启</button>   <button onclick=\"closeBeatThread('"+value.threadName+"')\">关闭</button>",
                     note:0
                 });
	        });
            return rows;
        }
    		
    		function pagerFilter(data){
    			if (typeof data.length == 'number' && typeof data.splice == 'function'){	// is array
    				data = {
    					total: data.length,
    					rows: data
    				}
    			}
    			var dg = $(this);
    			var opts = dg.datagrid('options');
    			var pager = dg.datagrid('getPager');
    			pager.pagination({
    				onSelectPage:function(pageNum, pageSize){
    					opts.pageNumber = pageNum;
    					opts.pageSize = pageSize;
    					pager.pagination('refresh',{
    						pageNumber:pageNum,
    						pageSize:pageSize
    					});
    					dg.datagrid('loadData',data);
    				}
    			});
    			if (!data.originalRows){
    				data.originalRows = (data.rows);
    			}
    			var start = (opts.pageNumber-1)*parseInt(opts.pageSize);
    			var end = start + parseInt(opts.pageSize);
    			data.rows = (data.originalRows.slice(start, end));
    			return data;
    		}
    		
    		$(function(){
   
    			 var AjaxURL="<%=basePath%>admin/user_getThread.action";
            	 $.ajax({
                 	url:AjaxURL,
                 	 type:"post",
                 	 datatype:"json",
                 	success:function(data){ 
                 		 var json = eval($.parseJSON(data)); //数组
                 		
                 		$('#dg').datagrid({loadFilter:pagerFilter}).datagrid('loadData', getData(json));       		 
                 			
                 		 
                 	},
                 	error:function(data){
                 	}
                 
                 });
            	
            		
    		});
    	</script>
    </body>
    </html>
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	<span>生命维持线程</span>
	<table width="%80">
		<tr>
			<td width="%20">线程名</td>
			<td width="%20">状态</td>
			<td width="%20">操作</td>
		</tr>

		<tbody id="thradlist">
			
		</tbody>
	</table>
	</br>
	</br>
	<span>心跳线程</span>
	<table width="%80">
		<tr>
			<td width="%20">线程名</td>
			<td width="%20">状态</td>
			<td width="%20">操作</td>
		</tr>

		<tbody id="beatthradlist">
			
		</tbody>
	</table>
</body>
</html>
